package com.youmans.minio.utils;
import com.youmans.minio.controller.MinioController;
import org.apache.commons.lang.ArrayUtils;
import org.apache.commons.lang.StringUtils;

import javax.imageio.ImageIO;
import java.awt.*;
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.util.ArrayList;
//@Component
public class FileUtils {
    public static ArrayList<String> listDir(File file)throws IOException {
        ArrayList<String> urlList =new ArrayList<>();
        File[] files = file.listFiles();
        if(!(files == null || "".equals(files))){
            for (File file1 : files) {
                if(file1.isDirectory()){//是文件夹
                    File[] files2 = file1.listFiles();
                    if(files2 !=null){
                        for (File file3 : files2) {
                            if(isImageBySuffix(file3.getName().substring(file3.getName().lastIndexOf(".") + 1))&&isImage(new FileInputStream(file3))){
                                urlList.add(file3.getPath());
                            }else{
                                if(file3.isDirectory()){
                                    listDir(file3);
                                }
                            }
                        }
                    }

                }else{
                    if(isImageBySuffix(file1.getName().substring(file1.getName().lastIndexOf(".") + 1))&&isImage(new FileInputStream(file1))){//判断是否为图片

                        urlList.add(file1.getPath());
//                        urlList.add(file1.getPath());
//                        System.err.println(file1.getName());
//                        System.out.println(urlList.size());
//                        System.err.println(file1.getPath());
                    }
                }
                //t01feb5c2038001b19a.jpg
//                System.out.println(file1.getName());
                //C:\Users\Administrator\Desktop\美女图片_360图片_files\美女_360图片2_files\t01feb5c2038001b19a.jpg
           // System.out.println(urlList);
            }
        }

        return urlList;
    }
    //判断文件是否为图片
    public static boolean isImage(InputStream inputStream) {
        if (inputStream == null) {
            return false;
        }
        Image img;
        try {
            img = ImageIO.read(inputStream);
            return !(img == null || img.getWidth(null) <= 0 || img.getHeight(null) <= 0);
        } catch (Exception e) {
            return false;
        }
    }

    public static boolean isImageBySuffix(String imageFileSuffix) {
        if (StringUtils.isNotEmpty(imageFileSuffix)) {
            //[JPG, jpg, bmp, BMP, gif, GIF, WBMP, png, PNG, wbmp, jpeg, JPEG]
            String[] formatNames = ImageIO.getReaderFormatNames();
            if (ArrayUtils.isNotEmpty(formatNames)) {
                for (String formatName : formatNames) {
                    if (imageFileSuffix.toLowerCase().equals(formatName)) {
                        return true;
                    }
                }
            }
        }
        return false;
    }
}
